<template>  
    <view class="container">  
		
		<view class="user-section">
			<image class="bg" src="/static/user-bg.jpg"></image>
			<view class="user-info-box" @click="gologin">
				<view class="portrait-box">
					<image class="portrait" :src="userInfo.avatar || '/static/missing-face.png'"></image>
				</view>
				<view class="info-box">
					<text class="username">{{userInfo.nickname || '未登录'}}</text>
				</view>
			</view>
	 
		</view>
		
		<view 
			class="cover-container"
			:style="[{
				transform: coverTransform,
				transition: coverTransition
			}]"
			@touchstart="coverTouchstart"
			@touchmove="coverTouchmove"
			@touchend="coverTouchend"
		>
			<image class="arc" src="/static/arc.png"></image>
			
			<view class="tj-sction">
				<view class="tj-item" @click="gopage('/pages/money/amountlog')">
					<text class="num">{{userInfo.money}}</text>
					<text>余额</text>
				</view>
				<view class="tj-item" @click="gopage('/pages/user/coupon')">
					<text class="num">{{userInfo.coupon_num}}</text>
					<text>优惠券</text>
				</view>
				<view class="tj-item" @click="gopage('/pages/money/scorelog')">
					<text class="num">{{userInfo.score}}</text>
					<text>积分</text>
				</view>
			</view>
			<!-- 订单 -->
			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="yticon icon-shouye"></text>
					<text>全部订单</text>
					
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=1')"  hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-daifukuan"></text>
					<text>待付款</text>
					<uni-badge class="bageflag" :text="waitpay_order" type="error" size="small" />
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="yticon icon-yishouhuo"></text>
					<text>待发货</text>
					<uni-badge class="bageflag" :text="waitsend_order" type="error" size="small" />
				</view>
				<view class="order-item" @click="navTo('/pages/order/orderservicelist')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>退款/售后</text>
					<uni-badge class="bageflag" :text="shouhou_order" type="error" size="small" />
				</view>
			</view>
		 <uni-list class="item-list">
 		 	<uni-list-item  show-extra-icon="true" showArrow :extra-icon="{color: '#fa436a',size: '22',type: 'list'}"   title="拼团订单" clickable @click="gopage('/pages/order/orderpinlist')"   />
			<uni-list-item  show-extra-icon="true" showArrow :extra-icon="{color: '#fa436a',size: '22',type: 'heart'}" clickable @click="gopage('/pages/product/fav')"   title="我的收藏"   />
	 
			<uni-list-item  show-extra-icon="true" showArrow :extra-icon="{color: '#5DD06D',size: '22',type: 'map'}" clickable @click="gopage('/pages/address/address')"    title="地址管理"   />

			<uni-list-item  show-extra-icon="true" showArrow :extra-icon="{color: '#3AB3F8',size: '22',type: 'gear-filled'}" clickable @click="gopage('/pages/set/set')"  title="通用设置"   />
		 </uni-list>
		 
	 
		 
		</view>
			
		
    </view>  
</template>  
<script>  
	 
   
	let startY = 0, moveY = 0, pageAtTop = true;
    export default {
	 
		data(){
			return {
				userInfo:{
					money:0,
					coupon_num:0,
					score:0,
					nickname:""
				},
				haslogin:false, 
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				waitpay_order:0, //小红点
				waitsend_order:0, //小红点
				shouhou_order:0//小红点
			}
		},
		onLoad(){
		
		},
		onShow() {
 
	   
			if(this.$util.getToken()){
				this.haslogin=true;
				 this.getUserinfo();
				 this.getOrderStatus();
			} else{
				this.userInfo={
					money:0,
					coupon_num:0,
					score:0,
					nickname:"",
				
				}
				this.waitpay_order=0;
				this.waitsend_order=0;
				this.shouhou_order=0;
			}
			
		},
        methods: {
			getOrderStatus()
			{
				let that = this;
				this.$util.req("/order/getOrderStatusSum",{},true,function(res){
					that.waitpay_order = res.data.waitpay_order;
					that.waitsend_order = res.data.waitsend_order;
					that.shouhou_order = res.data.shouhou_order;
				});
				
			},
			gologin(){
				if(!this.$util.getToken()){
					
					uni.navigateTo({
						url:"/pages/public/login"
					})
				}else{
					uni.navigateTo({
						url:"/pages/set/set"
					})
				}
			},
			bindwechat()
			{
				let that = this;
                  
				  	uni.login({
				  		provider: "weixin",
						onlyAuthorize:true,
				  		success(loginRes) {
				  			console.log(loginRes);
				  			that.$util.req("/user/bindWechat", {
				  				code: loginRes.code,
								channel:that.$runplatform
				  			}, true, function(res) {
				  			    console.log("bind wechat ok...");
							 
				  			});
				  		}
				  	});
			 
			 
				
			},
			getUserinfo(){
				let that = this;
				this.$util.req("/user/getInfo",{},true,function(res){
					that.userInfo = res.data;
					//判断是否有openid
					 
					if(that.$runplatform=="wechat" && res.data.wechat_bind==0){//微信下没有绑定微信需要绑定，支付时用
						console.log("start bind.. wechat");
						that.bindwechat();
					}
					
				},function(res){
				 	that.$util.logout();
					 that.$util.navigate("/pages/public/login",false);
				});
			},
			gopage(pageurl){ 
				
				this.$util.navigate(pageurl,true);
			},

			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url){

				this.$util.navigate(url,true);
				 
			}, 
	
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e){
				if(pageAtTop === false){
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e){
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if(moveDistance < 0){
					this.moving = false;
					return;
				}
				this.moving = true;
				if(moveDistance >= 80 && moveDistance < 100){
					moveDistance = 80;
				}
		
				if(moveDistance > 0 && moveDistance <= 80){
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend(){
				if(this.moving === false){
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
        }  
    }  
</script>  
<style lang='scss'>
 

	%flex-center {
	 display:flex;
	 flex-direction: column;
	 justify-content: center;
	 align-items: center;
	}
	%section {
	  display:flex;
	  justify-content: space-around;
	  align-content: center;
	  background: #fff;
	  border-radius: 10upx;
	}

	.user-section{
		height: 520upx;
		padding: 100upx 30upx 0;
		position:relative;
		.bg{
			position:absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
	}
	.bageflag{
		position: absolute;
		top: 0upx;
		right: -12upx;
	}
	.user-info-box{
		height: 180upx;
		display:flex;
		align-items:center;
		position:relative;
		z-index: 1;
		.portrait{
			width: 130upx;
			height: 130upx;
			border:5upx solid #fff;
			border-radius: 50%;
		}
		.username{
			font-size: $font-lg + 6upx;
			color: $font-color-dark;
			margin-left: 20upx;
		}
	}

	 
	.cover-container{
		background: $page-color-base;
		margin-top: -150upx;
		padding: 0 30upx;
		position:relative;
		background: #f5f5f5;
		padding-bottom: 20upx;
		.arc{
			position:absolute;
			left: 0;
			top: -34upx;
			width: 100%;
			height: 36upx;
		}
	}
	.tj-sction{
		@extend %section;
		.tj-item{
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #75787d;
		}
		.num{
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}
	.order-section{
		@extend %section;
		padding: 28upx 0;
		margin-top: 20upx;
		.order-item{
			position: relative;
			@extend %flex-center;
			width: 120upx;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;
		}
		.yticon{
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #fa436a;
		}
		.icon-shouhoutuikuan{
			font-size:44upx;
		}
	}
 
	
</style>